#pragma strict

var Target : Transform;
var CameraDistance = 15;
var CameraSpeed = 5;
var fadeTexture : Texture;
var isLoading : boolean;
var fadeTime : float;
var alphaFadeValue : float;
var firstLoad : boolean;
var menuClicked : boolean;

function Start(){

	alphaFadeValue = 1;
	isLoading = false;
	fadeTime = 2;
	firstLoad = true;
	menuClicked = false;

}

function Update(){
	if(GameObject.Find("Player")){
		transform.position = Vector3(Mathf.MoveTowards(transform.position.x, Target.position.x, CameraSpeed * Time.smoothDeltaTime),
									 Mathf.MoveTowards(transform.position.y, Target.position.y + 2, CameraSpeed * Time.smoothDeltaTime),
									 (Target.position.z + CameraDistance));
	} else { return; }
}

function OnGUI(){

	if(firstLoad){
		alphaFadeValue -= Mathf.Clamp01(Time.deltaTime / fadeTime);
		
		GUI.color = Color(0,0,0,alphaFadeValue);
		GUI.DrawTexture(Rect(0,0,Screen.width, Screen.height),fadeTexture);
		
		if(alphaFadeValue <= 0){
			fadeTime = 0;
			alphaFadeValue = 0;
			firstLoad = false;
			GameObject.Find("Player").GetComponent(CharacterMotor).hasStarted = true;
		}
	}
	
	if(menuClicked){
	
		fadeTime = 2;
		alphaFadeValue += Mathf.Clamp01(Time.deltaTime / fadeTime);
		
		GUI.color = Color(0,0,0,alphaFadeValue);
		GUI.DrawTexture(Rect(0,0,Screen.width, Screen.height),fadeTexture);
			
		if(alphaFadeValue >= 1){
			Application.LoadLevel("MainMenu");
		}

	}
	
	if(firstLoad){
		alphaFadeValue -= Mathf.Clamp01(Time.deltaTime / fadeTime);
		
		GUI.color = Color(0,0,0,alphaFadeValue);
		GUI.DrawTexture(Rect(0,0,Screen.width, Screen.height),fadeTexture);
		
		if(alphaFadeValue <= 0){
			fadeTime = 0;
			alphaFadeValue = 0;
			firstLoad = false;
			GameObject.Find("Player").GetComponent(CharacterMotor).hasStarted = true;
		}
	}



}